<template>
  <template v-for="(item,index) in props.menuData">
    <!--没有子菜单-->
    <el-menu-item
        v-if="!item.children || item.children.length === 0"
        :index="`${props.index}-${item.meta.id}`"
        :key="`${props.index}-${item.meta.id}`"
        @click="handleClick(item,`${props.index}-${item.meta.id}`)">
      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <!--包含有子菜单-->
    <el-sub-menu v-else
                 :index="`${props.index}-${item.meta.id}`"
                 :key="`${props.index}-${item.meta.id}`"
    >
      <template #title>
        <el-icon size="20">
          <component :is="item.meta.icon"></component>
        </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <!--子菜单的递归处理-->
      <TreeMenu :index="`${props.index}-${item.meta.id}`" :menu-data="item.children"/>
    </el-sub-menu>
  </template>

</template>
<script setup>
import {useRouter} from 'vue-router'
import store from "../store/index.js";

const props = defineProps(['menuData', 'index'])
const router = useRouter()
console.log(props, 'props')
//点击菜单
const handleClick = (item, active) => {
  router.push(item.meta.path)
  store.commit('addMenu', item.meta)
  store.commit('updateMenuActive', active)
}

</script>
<style scoped>

</style>
